from rest_framework.authtoken.models import Token
from apps.common.base.serializers import *
from apps.system.user.models import *

class TokenBaseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Token
        fields = '__all__'

class TokenModelSerializer(TokenBaseSerializer):

    class Meta:
        model = Token
        fields = '__all__'

class UserBaseSerializer(BaseSerializer):
    password = serializers.CharField(required=False, allow_blank=True)
    class Meta:
        model = UserModel
        fields = "__all__"

# 管理端用户列表
class UserModelSerializer(BaseSerializer):

    rankModel = serializers.SerializerMethodField()
    def get_rankModel(self, obj):
        try:
            return {'id': obj.rank.id, 'grade': obj.rank.grade, 'name': obj.rank.name}
        except:
            return {}
        
    teamModel = serializers.SerializerMethodField()
    def get_teamModel(self, obj):
        try:
            return {'id': obj.team.id, 'grade': obj.team.grade, 'name': obj.team.name}
        except:
            return {}
        
    identityModel = serializers.SerializerMethodField()
    def get_identityModel(self, obj):
        try:
            return {'id': obj.identity.id, 'grade': obj.identity.grade, 'name': obj.identity.name}
        except:
            return {}

    roleModel = serializers.SerializerMethodField()
    def get_roleModel(self, obj):
        try:
            return {'id': obj.role.id, 'key': obj.role.key, 'name': obj.role.name}
        except:
            return {}
        
    parentModel = serializers.SerializerMethodField()

    def get_parentModel(self, obj):
        try:
            return {'id': obj.parent.id, 'username': obj.parent.username, 'nickname': obj.parent.nickname,
                    'phone': obj.parent.phone, 'code': obj.parent.code, }
        except:
            return {}

    genderLabel = serializers.SerializerMethodField()
    def get_genderLabel(self, obj):
        if obj.gender:
            return obj.get_gender_display()
        else:
            return '未知'

    activityPerformanceList = serializers.SerializerMethodField()
    def get_activityPerformanceList(self, obj):
        try:
            return [{'id': obj.id, 'activityKey': obj.activity.key,
                     'verticalOrderNumber': obj.verticalOrderNumber, 'verticalPerformance': str(obj.verticalPerformance),
                     'teamOrderNumber': obj.teamOrderNumber, 'teamPerformance': str(obj.teamPerformance)} for obj in obj.user_ActivityPerformance.all()] if obj.user_ActivityPerformance.all() else [{'verticalOrderNumber': 0, 'verticalPerformance': str(0), 'teamOrderNumber': 0, 'teamPerformance': str(0)}]
        except:
            return [{'verticalOrderNumber': 0, 'verticalPerformance': str(0),
                     'teamOrderNumber': 0, 'teamPerformance': str(0)}]

    class Meta:
        model = UserModel
        fields = "__all__"

# 用户详情
class UserModelDetailSerializer(UserModelSerializer):

    class Meta:
        model = UserModel
        fields = "__all__"

# tree
class UserTreeModelSerializer(serializers.ModelSerializer):

    children = serializers.SerializerMethodField()
    def get_children(self, obj):
        return [{}]

    label = serializers.SerializerMethodField()
    def get_label(self, obj):
        print('obj', obj.id)
        return obj.code + ' - ' + obj.phone + ' - ' + (obj.rank.name if obj.rank else '') + ((' - ' + obj.nickname) if obj.nickname else '')

    class Meta:
        model = UserModel
        fields = ('id', 'code', 'phone', 'nickname', 'realname', 'label', 'children')

# 管理员列表
class AdminModelSerializer(BaseSerializer):


    roleModel = serializers.SerializerMethodField()
    def get_roleModel(self, obj):
        try:
            return {'id': obj.role.id, 'name': obj.role.name, 'key': obj.role.key}
        except:
            return {}

    class Meta:
        model = UserModel
        fields = ('id', 'lessee', 'username', 'realname', 'nickname', 'phone', 'sort', 'createTime', 'role', 'roleModel',
                  'status')
